package org.bluedream.core.module.sys.service.dict;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.base.TreeEntity;
import org.bluedream.comm.utils.CreateGUIDUtil;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.sys.dao.dict.DictDataDao;
import org.bluedream.core.module.sys.entity.dict.DictData;
import org.bluedream.core.module.sys.entity.dict.DictType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DictDataService
 * @Description TODO
 * @Author foxsand
 * @Data 2021-09-09 16:52
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class DictDataService extends OrgBaseServiceImpl<DictDataDao , DictData> implements IService<DictData> {
    private static final Logger logger = LoggerFactory.getLogger(DictDataService.class);

    @Override
    public DictData getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new DictData();
        }
        return super.getById(id);
    }

    public IPage<DictData> findPage(DictData entity, String... args) {
        if (EmptyUtil.isEmpty(entity.getDictType())){
            throw new RuntimeException("没有字典类型");
        }
        Map<String , Object> search = new HashedMap();
        search.put("dict_type;eq" , entity.getDictType());
        search.put("org_code;eq" , entity.getOrgCode());
        if (EmptyUtil.isNoEmpty(entity.getDictLabel())
                || EmptyUtil.isNoEmpty(entity.getDictValue())
        ){
            if (EmptyUtil.isNoEmpty(entity.getDictLabel())){
                search.put("dict_label" , entity.getDictLabel());
            }
            if (EmptyUtil.isNoEmpty(entity.getDictValue())){
                search.put("dict_value" , entity.getDictValue());
            }
        }else {
            long limit = entity.getLimit();
            long page = entity.getPage();
            entity = new DictData();
            entity.setLimit(limit);
            entity.setPage(page);
        }
        return super.findPage(entity , search , args);
    }

    public DictData getDictData(DictData entity){
        int _sort = TreeEntity.DEFAULT_TREE_SORT;
        DictData lastData = this.getLastData(entity);
        if (EmptyUtil.isNoEmpty(lastData)){
            _sort = lastData.getSort() + TreeEntity.TREE_SORTS_LENGTH;

        }
        entity.setSort(_sort);
        return entity;
    }

    public boolean save(DictData entity){
        System.out.println("org Code = " + entity.getOrgCode());
        try {
            if (EmptyUtil.isEmpty(entity.getDictCode())){
                String dictCode = "001";
                DictData lastData = this.getLastData(entity);
                if (EmptyUtil.isNoEmpty(lastData)){
                    dictCode = CreateGUIDUtil.nextCode(lastData.getDictCode());
                }
                entity.setDictCode(dictCode);
            }
            return super.save(entity);
        }catch (Exception e){
            logger.error("保存菜单信息失败，错误信息：" + e.getMessage());
            return false;
        }
    }

    private DictData getLastData(DictData entity){
        String dictType = entity.getDictType();
        Page<DictData> page = new Page<>(1,1);
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq("dict_type" , dictType);
        qw.orderByDesc("sort");
        IPage<DictData> iPage = super.page(page , qw);
        if (EmptyUtil.isNoEmpty(iPage.getRecords())){
            return iPage.getRecords().get(0);
        }
        return null;
    }
}
